package java6681.shopping;

import java6681.lesson01.Market6681;
import java6681.lesson01.Person6681;
import java6681.lesson01.Product6681;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;

/**
 * @author Junichiro
 * @description
 * @date 2021-03-16 14:10
 **/

public class ShoppingWin6681 extends Application {
    private TextField tfPerson;
    private TextField tfBuy;
    private Label lblGet;
    private Market6681 market;
    private Button button;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
//        VBox垂直布局
        VBox root = new VBox(20);
//         添加文本
        Label lblStudent = new Label("212006681|张舒婷");
//        文本边框 颜色，虚线,四角的半径，宽设置
        BorderStroke bs = new BorderStroke(Paint.valueOf("#8FBC8F")
                , BorderStrokeStyle.DOTTED
                , new CornerRadii(10)
                , new BorderWidths(3));
//        将设置添加进窗口
        lblStudent.setBorder(new Border(bs));
//        边框距离
        lblStudent.setPadding(new Insets(10));

//        HBox水平布局
        HBox hPerson = new HBox(10);
        Label lblPerson = new Label("购物者：");
//        输入框
        tfPerson = new TextField();
//        输入框里的文字
        tfPerson.setPromptText("购物者姓名");
//        添加进水平布局
        hPerson.getChildren().addAll(lblPerson, tfPerson);

        HBox hBuy = new HBox(10);
        Label lblBuy = new Label("购买商品清单：");
        tfBuy = new TextField();
        tfBuy.setPrefWidth(420);
        tfBuy.setPromptText("用逗号分隔的商品名称");
//        添加进水平布局
        hBuy.getChildren().addAll(lblBuy, tfBuy);

//          按钮
        button = new Button("购买");
//        按钮下的文本设置
        lblGet = new Label();

//        整体添加进VBox垂直布局中
        root.getChildren().addAll(lblStudent, hPerson, hBuy, button, lblGet);
        root.setPadding(new Insets(20.0));
//        居中
        root.setAlignment(Pos.TOP_CENTER);
//        VBox添加进场景-窗体设置宽高
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.setTitle("超市购物");
        primaryStage.show();

        /*创建超市对象*/
        createMarket();
        /*注册监听*/
        button.setOnAction(new MyEvenHandler());
    }

    /*创建超市对象*/
    private void createMarket() {
        /*准备超市可销售的商品清单*/
        Product6681 one = new Product6681("摄像头");
        Product6681 two = new Product6681("电视机");
        Product6681 three = new Product6681("键盘");
        Product6681 four = new Product6681("笔记本");
        Product6681[] pList = new Product6681[]{one, two, three, four, new Product6681("可乐")};

        /*创建超市对象*/
        market = new Market6681("永辉", pList);
    }

    /*根据输入的内容创建购物者对象*/
    private Person6681 createPerson(String strInput) {
        /*检查是否全是空格*/
        String name = strInput.trim();
        if (name.isEmpty()) {
            lblGet.setText("输入购物者姓名");
            return null;
        }
        return new Person6681(name);
    }

    /*根据输入的内容创建模拟购买商品清单*/
    private Product6681[] createBuyList(String strInput) {
        /*检查是否为全空格*/
        if (strInput.trim().isEmpty()) {
            lblGet.setText("请输入用逗号分隔的商品名称");
            return null;
        }
        /*创建需要购买的商品清单*/
        String[] strBuyNames = strInput.trim().split(",");
        Product6681[] buyProducts = new Product6681[strBuyNames.length];
        int k = 0;
        String productName = null;
        for (int i = 0; i < strBuyNames.length; i++) {
             productName = strBuyNames[i].trim();
             if (!productName.isEmpty()){
                 /*有效商品名称*/
                 buyProducts[k++] = new Product6681(productName);
             }
        }
        if (k == 0){
            lblGet.setText("请输入用逗号分隔的商品名称");
            return null;
        }
        return buyProducts;
    }
    class MyEvenHandler implements EventHandler<ActionEvent> {
        @Override
        public void handle(ActionEvent event) {
            /*创建购物者*/
            Person6681 person = createPerson(tfPerson.getText());
            if (person==null){
                return;
            }
            /*创建拟购买的商品清单*/
            Product6681[] buyProducts = createBuyList(tfBuy.getText());
            if (buyProducts==null){
                return;
            }
            /*调用购物者的shopping方法购买商品*/
            String strGetProducts = person.shopping(market, buyProducts);
            /*显示结果*/
            if (strGetProducts.isEmpty()){
                lblGet.setText(person + "白跑一趟，在" + market + "什么都没有买到");
            }else {
                lblGet.setText(person + "在" + market + "买到了:" + strGetProducts);
            }
        }
    }
}
